package com.njit.office.service.impl;

import com.njit.office.dao.LoginUserMapper;
import com.njit.office.dao.TaskMapper;
import com.njit.office.dao.TeamMapper;
import com.njit.office.dataobject.Task;
import com.njit.office.form.AddTaskForm;
import com.njit.office.model.Result;
import com.njit.office.model.TaskModel;
import com.njit.office.model.TaskState;
import com.njit.office.service.TaskService;
import com.njit.office.utils.IDUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class TaskServiceImpl implements TaskService {

    @Resource
    TaskMapper taskMapper;

    @Resource
    TeamMapper teamMapper;

    @Resource
    LoginUserMapper loginUserMapper;

    @Override
    public Result add(AddTaskForm addTaskForm) {

        Result result = new Result();
        Task task = new Task();
        task.setId(IDUtils.toUUID());
        task.setContent(addTaskForm.getContent());
        task.setDeadline(AddTaskForm.praseDate(addTaskForm.getDeadLine()));
        task.setIsUrgent(addTaskForm.isUrgent());
        task.setState(TaskState.UNFINISHED.name());
        task.setTeamId(addTaskForm.getTeamId());
        task.setUserId(addTaskForm.getUserId());
        taskMapper.insert(task);
        result.setSuccess(true);
        return result;
    }

    @Override
    public Result getTask(String userId) {

        Result result = new Result();

        //原始数据
        List<Task> tasks = taskMapper.selectByUserId(userId);
        //model
        List<TaskModel> taskModels = new ArrayList<>();

        for (Task task : tasks) {
            TaskModel taskModel = new TaskModel();
            taskModel.setContent(task.getContent());
            taskModel.setDeadLine(task.getDeadline());
            taskModel.setTaskId(task.getId());
            taskModel.setTaskState(task.getState());
            taskModel.setTeam(teamMapper.selectByPrimaryKey(task.getTeamId()));
            taskModel.setUrgent(task.getIsUrgent());
            taskModel.setLoginUser(loginUserMapper.selectByPrimaryKey(task.getUserId()));
            taskModels.add(taskModel);
        }
        result.setData(taskModels);

        return result;
    }

    @Override
    public Result deleteTask(String taskId) {

        Result result = new Result();

        int sqlResult = taskMapper.deleteByPrimaryKey(taskId);

        if (sqlResult > 0) {
            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);
        result.setErrMsg("删除task失败");
        return result;

    }

    @Override
    public Result updateTaskState(String taskId, TaskState taskState) {

        Result result = new Result();

        Task task = new Task();
        task.setId(taskId);
        task.setState(taskState.name());

        taskMapper.updateTaskState(task);

        result.setSuccess(true);
        return result;
    }

    @Override
    public Result urgentTask(String taskId) {

        Result result = new Result();

        Task task = new Task();
        task.setId(taskId);
        task.setIsUrgent(true);
        taskMapper.updateTaskUrgent(task);

        result.setSuccess(true);
        return result;
    }

    @Override
    public Result delTask(String taskId) {

        Result result = new Result();

        taskMapper.deleteByPrimaryKey(taskId);

        result.setSuccess(true);
        return result;
    }
}
